package com.yfux._05_tlias.controller;

import com.yfux._05_tlias.pojo.Emp;
import com.yfux._05_tlias.pojo.Result;
import com.yfux._05_tlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    EmpService empService;

    @GetMapping
    public Result list(String name,
                       Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询分页数据, name={}, gender={}, begin={}, end={}, page={}, pageSize={}",
                name, gender, begin, end, page, pageSize);
        return Result.success(empService.list(name, gender, begin, end, page, pageSize));
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("删除, ids={}", ids);
        int rows = empService.delete(ids);
        return rows > 0 ? Result.success() : Result.error("删除员工失败, ids=" + ids);
    }

    @PostMapping
    public Result insert(@RequestBody Emp emp) {
        log.info("插入员工数据: {}", emp);
        int rows = empService.insert(emp);
        return rows > 0 ? Result.success() : Result.error("添加员工失败: " + emp);
    }
}
